<template>
  <van-popup round v-model="show_sub_add" @close="close_pop">
    <div class="form">
      <div class="title">
        <div class="c_flex" style="padding:10px">新增加盟车</div>
      </div>
      <van-form validate-trigger="onSubmit" validate-first @submit="onSubmit">
        <van-field autocomplete="off" v-model="car_raw	" name="car_raw" placeholder="车牌号"
          :rules="[{validator: valid_car,message: '请输入正确的车牌号(例如一般车 豫GA2334、新能源为 豫GA62334)'}]">
          <template #left-icon>
            <svg-icon class-name="icon_size" icon-class="car" />
          </template>

        </van-field>
        <div class="submit_out c_flex">
          <van-button native-type="submit" class="submit">确定</van-button>
        </div>
      </van-form>
    </div>
  </van-popup>
</template>

<script>
  import {
    RegionService,
    ShoperService
  } from '@/api';
  export default {
    name: 'add_car',
    props: {
      show_add: Boolean,
      shoper_id: String,
    },
    computed: {
      show_sub_add: {
        get() {
          return this.show_add
        },
        set(value) {
          console.log(value);
          if (!value) {
            this.close_pop();
          }
        }
      }
    },
    mounted() {
      RegionService.getRegion({
        max_leve: 1
      }).then(res => {
        this.regions = res.data;
      })
    },
    data() {
      return {
        regions: [],
        user_data: {
          plate: '',
          province_id: '',
        },
        car_raw: '',
      }
    },
    watch: {
      car_raw(val) {
        var result = /^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领])([A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳])$/.test(
          val)
        if (result) {
          this.regions.forEach(item => {

            if (item.short_name == RegExp.$1) {
              this.user_data.province_id = item.id;
            }
          });
          this.user_data.plate = RegExp.$2;
        }
      }
    },
    methods: {
      valid_car(value) {
        return /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/.test(value)
      },
      onSubmit() {
        ShoperService.addCar(this.user_data, this.shoper_id).then(res => {
          console.log(res)
          this.$EventBus.$emit("update_one")
          this.close_pop();
        });
      },
      close_pop() {
        this.empty();
        this.$emit('update:show_add', false);
      },
      empty() {
        this.user_data = {
          plate: '',
          province_id: '',
        }
        this.car_raw = '';
      }
    },
  }
</script>

<style lang="less" scoped>
  .icon_size {
    width: 30px;
    height: 30px;
    margin-right: 15px;
  }

  .form {
    .title {
      width: 50%;
      border-bottom: 1px solid rgba(153, 153, 153, 1);
      margin: 0 auto 15px;
    }

    width:450px;


    .submit_out {
      margin-top: 40px;
      margin-bottom: 30px;
    }

    .submit {

      width: 360px;
      height: 70px;
      border-radius: 37.84px;
      background-color: rgba(235, 95, 17, 1);
      letter-spacing: 1.8px;
      font-size: 28px;
      color: rgba(255, 255, 255, 1);
    }
  }
</style>